package com.rememberme;

import java.io.IOException;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.WindowManager.LayoutParams;

import com.dataobject.MyReminder;

public class AlarmDialog extends DialogFragment {
	private MyReminder _MyMr;
	private MediaPlayer mMediaPlayer;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		_MyMr = new MyReminder();
		_MyMr.set_Ico(getArguments().getString("ico"));
		_MyMr.set_Title(getArguments().getString("title"));
		_MyMr.set_Content(getArguments().getString("content"));
		_MyMr.set_Alarm(getArguments().getString("alarm"));
		_MyMr.set_Tone(getArguments().getString("tone"));
		if (!_MyMr.get_Alarm().equals(
				getResources().getString(R.string.alarm_silent))) {
			if (!_MyMr.get_Tone().equals(
					getResources().getString(R.string.tone_default))) {
				RingtoneManager.getRingtone(getActivity(),
						Uri.parse(_MyMr.get_Tone())).play();
			} else {

				Uri alert = RingtoneManager
						.getDefaultUri(RingtoneManager.TYPE_ALARM);
				mMediaPlayer = new MediaPlayer();
				try {
					mMediaPlayer.setDataSource(getActivity()
							.getApplicationContext(), alert);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				final AudioManager audioManager = (AudioManager) getActivity()
						.getSystemService(Context.AUDIO_SERVICE);
				if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
					mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
					mMediaPlayer.setLooping(true);
					try {
						mMediaPlayer.prepare();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					mMediaPlayer.start();
				}
			}
		}
		getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setTitle(_MyMr.get_Title());
		builder.setMessage(_MyMr.get_Content());
		builder.setIcon(getResources().getDrawable(
				getResources().getIdentifier(_MyMr.get_Ico(), "drawable",
						getActivity().getPackageName())));
		builder.setPositiveButton("OK", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				mMediaPlayer.stop();
				getActivity().finish();
			}
		});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().finish();
	}
}
